package com.cxm.qyyz.utils.flow;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.cxm.qyyz.R;



/**
 * @author zwl
 * @describe 折叠
 * @date on 2021/8/7
 */
public class JDFoldLayout extends FlowListView {


    public JDFoldLayout(Context context) {
        this(context, null);
    }

    public JDFoldLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public JDFoldLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        upFoldView = LayoutInflater.from(context).inflate(R.layout.view_item_fold_up, null);
        downFoldView = LayoutInflater.from(context).inflate(R.layout.view_item_fold_down, null);
        upFoldView.setOnClickListener(v -> {
            mFold = false;
            flowAdapter.notifyDataChanged();
        });

        downFoldView.setOnClickListener(v -> {
            mFold = true;
            flowAdapter.notifyDataChanged();
        });

    }

    /**
     * 折叠状态改变回调
     *
     * @param canFold
     * @param newFoldState
     */
    public void changeFold(boolean canFold, boolean newFoldState, int index, int surplusWidth) {
        super.changeFold(canFold,newFoldState,index,surplusWidth);

        if (canFold) {
            if (newFoldState) {
                int upIndex = index(index, surplusWidth);
                int childCount = getChildCount();
                if (upIndex>childCount){
                    Utils.removeFromParent(upFoldView);
                    addView(upFoldView);
                }else {
                    for (int i=childCount; i >= upIndex; i--){
                        removeView(getChildAt(i));
                    }
                    Utils.removeFromParent(upFoldView);
                    addView(upFoldView);
                }
            } else {
                Utils.removeFromParent(downFoldView);
                addView(downFoldView);
                if (null!=listener)listener.onFold(true);
            }
        }
    }
    public interface FoldListener {
        void onFold(boolean isOpen);
    }

    public FoldListener getListener() {
        return listener;
    }

    public void setListener(FoldListener listener) {
        this.listener = listener;
    }

    private FoldListener listener;
    private int index(int index, int surplusWidth) {
        int upIndex = index;
        int upWidth = Utils.getViewWidth(upFoldView);
        //当剩余空间大于等于展开View宽度直接加入index+1
        if (surplusWidth<0)
            surplusWidth = -surplusWidth;
        int right = surplusWidth;
        //当剩余空间大于等于展开View宽度直接加入index+1
        if (surplusWidth >= upWidth) {
            upIndex = index + 1;
        } else { //找到对应的位置
            for (int i = index; i >= 0; i--) {
                View view = getChildAt(i);
                int viewWidth = Utils.getViewWidth(view);
                right = right + viewWidth;
                if (upWidth <= right) {
                    return  i;
                }
            }
        }
        return upIndex;
    }

}
